package com.willian.fplugin;

import androidx.annotation.NonNull;

import com.willian.fplugin.utils.ConstraintsMap;

import java.util.HashMap;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FpluginPlugin */
public class FpluginPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  private EventChannel eventChannel;
  private EventChannel.EventSink eventSink;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    //注册方法通道
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "fplugin.method");
    channel.setMethodCallHandler(this);

    //注册事件通道
    eventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(),"fplugin.event");
    eventChannel.setStreamHandler(new EventChannel.StreamHandler(){
      @Override
      public void onListen(Object arguments, EventChannel.EventSink events) {
        eventSink = events;
      }

      @Override
      public void onCancel(Object arguments) {
        eventSink = null;
      }
    });
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if(call.method.equals("getNativeValue")){
      String tmpMessage = call.argument("message");
      System.out.println("安卓本地打印:" + tmpMessage);

      //原生调用flutter的方法
      invokeFlutterMethod_toFlutter();

      //返回值到flutter
      result.success(tmpMessage + "From Android");
    } else {
      result.notImplemented();
    }
  }

  public String toJava(String name){
    System.out.println("传递的参数是" + name);
    //原生调用flutter的方法
    invokeFlutterMethod_toFlutter();
    return "您好" + name;
  }

  public void invokeFlutterMethod_toFlutter(){
    //传递给flutter的数据，用对象包裹起来
    ConstraintsMap resMap = new ConstraintsMap();
    resMap.putString("item1","android value1");
    resMap.putString("item2","android value2");
    //"我是原生Android，我将参数传递给Flutter里面的一个方法"

    //通过原生的事件向flutter传值
    eventSink.success(resMap.toMap());

//    //通过调用flutter的方法向flutter传值
//    if (this.channel != null) {
//      this.channel.invokeMethod("fluMethod", resMap.toMap(), new MethodChannel.Result() {
//        @Override
//        public void success(Object o) {
//          //这里是flutter 成功的回调
//          System.out.println("这里是调用 flutter 成功的回调 " + o);
//        }
//        @Override
//        public void error(String s, String s1, Object o) {
//          //这里是flutter 失败的回调
//          System.out.println("这里是调用 flutter 失败的回调 " + o);
//        }
//        @Override
//        public void notImplemented() {
//
//        }
//      });
//    }

  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
